package com.zh.note.huawei.twohundred.去除多余空格;

import java.util.Arrays;

/**
 * @Author: zhanghuan
 * @date: 2023/8/9 08:58
 * @description: create it
 */
public class Main {
    /**
     * 找出关键字是哪些字符串，以空格分割原本字符串
     * 将输入的字符串变成字符数组，遍历字符数组，遇到单引号结束本次循环，遇到当前字符与前一个字符都是空格结束本次循环，没有遇到则加入StringBuilder
     * 最终在StringBuilder变成的字符串中找到关键字的位置。
     * @param args
     */
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String inputValue = sc.nextLine();

        String inputValue = "Life is painting a  picture, not doing 'a  sum'.";
        String index = "8 15,20 26,43 45";

        if (inputValue == null || inputValue.length() == 0 || inputValue.length() > 100000) {
            System.out.println("input error");
            return;
        }
        // String index = sc.nextLine();
        String[] splitIndex = index.split(",");
        //1.找出关键字
        String[] values = new String[splitIndex.length];
        for (int i = 0; i < splitIndex.length; i++) {
            String value = splitIndex[i];
            String[] idxs = value.split(" ");
            int leftIndex = Integer.parseInt(idxs[0]);
            int rightIndex = Integer.parseInt(idxs[1]);
            values[i] = inputValue.substring(leftIndex, rightIndex + 1);
        }
        StringBuilder sb = new StringBuilder();
        char[] charValue = inputValue.toCharArray();
        boolean flag = false;
        for (int j = 0; j < charValue.length; j++) {
            char ch = charValue[j];
            if (ch == '\'') {
                flag = !flag;
            }
            if (flag) {
                sb.append(ch);
                continue;
            }
            if (ch == ' ' && charValue[j - 1] == ' ') {
                continue;
            }
            sb.append(ch);
        }
        System.out.println(sb);
        for (String value : values) {
            int startIndex = sb.indexOf(value);
            int endIndex = startIndex + value.length() - 1;
            String res = Arrays.toString(new int[]{startIndex, endIndex});
            System.out.print(res);
        }


    }
}
